package com.qf.cx.simplenms.reader.tag;

import java.util.HashMap;
import java.util.Map;

import com.qf.cx.simplenms.common.utils.IntegerUtils;
import com.qf.cx.simplenms.link.model.ReadCustomResultSpec;
import com.qf.cx.simplenms.tagmemory.model.TagMemoryModel.Identifier;

public class Tag {
	private String tid;
	private long discoveryTime;
	private final Map<Identifier, VehicleInfo> vehicleInfos = new HashMap<>();
	private Map<Identifier, ReadOpResult> readOpResults = new HashMap<>();

	public String getTid() {
		return tid;
	}

	public void setTid(String tid) {
		this.tid = tid;
	}

	public long getDiscoveryTime() {
		return discoveryTime;
	}

	public void setDiscoveryTime(long discoveryTime) {
		this.discoveryTime = discoveryTime;
	}

	public Map<Identifier, VehicleInfo> getVehicleInfos() {
		return vehicleInfos;
	}

	public void addVehicleInfo(Identifier indentifier, VehicleInfo vehicleInfo) {
		vehicleInfos.put(indentifier, vehicleInfo);
	}

	public Map<Identifier, ReadOpResult> getReadOpResults() {
		return readOpResults;
	}

	public void setReadOpResults(Map<Identifier, ReadOpResult> readOpResults) {
		this.readOpResults = readOpResults;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((tid == null) ? 0 : tid.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (getClass() != obj.getClass()) {
			return false;
		}
		Tag other = (Tag) obj;
		if (tid == null) {
			if (other.tid != null) {
				return false;
			}
		} else if (!tid.equals(other.tid)) {
			return false;
		}
		return true;
	}

	public static class VehicleInfo {
		private String cid;
		private String license;
		private Integer usage;
		private String manufactureDate;
		private String vehicleType;
		private Integer power;
		private Integer emission;
		private Integer licenseType;
		private String validity;
		private Integer retirementDate;
		private Integer capacity;
		private Integer color;

		public String getCid() {
			return cid;
		}

		public void setCid(String cid) {
			this.cid = cid;
		}

		public String getLicense() {
			return license;
		}

		public void setLicense(String license) {
			this.license = license;
		}

		public Integer getUsage() {
			return usage;
		}

		public void setUsage(Integer usage) {
			this.usage = usage;
		}

		public String getManufactureDate() {
			return manufactureDate;
		}

		public void setManufactureDate(String manufactureDate) {
			this.manufactureDate = manufactureDate;
		}

		public String getVehicleType() {
			return vehicleType;
		}

		public void setVehicleType(String vehicleType) {
			this.vehicleType = vehicleType;
		}

		public Integer getPower() {
			return power;
		}

		public void setPower(Integer power) {
			this.power = power;
		}

		public Integer getEmission() {
			return emission;
		}

		public void setEmission(Integer emission) {
			this.emission = emission;
		}

		public Integer getLicenseType() {
			return licenseType;
		}

		public void setLicenseType(Integer licenseType) {
			this.licenseType = licenseType;
		}

		public String getValidity() {
			return validity;
		}

		public void setValidity(String validity) {
			this.validity = validity;
		}

		public Integer getRetirementDate() {
			return retirementDate;
		}

		public void setRetirementDate(Integer retirementDate) {
			this.retirementDate = retirementDate;
		}

		public Integer getCapacity() {
			return capacity;
		}

		public void setCapacity(Integer capacity) {
			this.capacity = capacity;
		}

		public Integer getColor() {
			return color;
		}

		public void setColor(Integer color) {
			this.color = color;

		}

		public static com.qf.cx.simplenms.reader.tag.Tag.VehicleInfo from(ReadCustomResultSpec spec) {
			com.qf.cx.simplenms.link.model.VehicleInfo t = new com.qf.cx.simplenms.link.model.VehicleInfo();
			t.setCcrq(spec.getCcrq());
			t.setCid(spec.getCid());
			t.setCllx(spec.getCllx());
			t.setCsys(spec.getCsys());
			t.setFpdh(spec.getFpdh());
			t.setGl(spec.getGl());
			t.setHphmxh(spec.getHphmxh());
			t.setHpzl(spec.getHpzl());
			t.setJyyxq(spec.getJyyxq());
			t.setPl(spec.getPl());
			t.setQzbfq(spec.getQzbfq());
			t.setSyxz(spec.getSyxz());
			t.setZkzl(spec.getZkzl());
			t.setCph();

			VehicleInfo v = new VehicleInfo();
			v.setCid(t.getCid());
			v.setCapacity(IntegerUtils.parseInt(t.getZkzl()));
			v.setColor(IntegerUtils.parseInt(t.getCsys()));
			v.setEmission(IntegerUtils.parseInt(t.getPl()));
			v.setLicense(t.getCph());
			v.setLicenseType(IntegerUtils.parseInt(t.getHpzl()));
			v.setManufactureDate(t.getCcrq());
			v.setPower(IntegerUtils.parseInt(t.getGl()));
			v.setRetirementDate(IntegerUtils.parseInt(t.getQzbfq()));
			v.setUsage(IntegerUtils.parseInt(t.getSyxz()));
			v.setValidity(t.getJyyxq());
			v.setVehicleType(t.getCllx());
			return v;
		}

		@Override
		public String toString() {
			return "VehicleInfo [cid=" + cid + ", license=" + license + ", usage=" + usage + ", manufactureDate="
					+ manufactureDate + ", vehicleType=" + vehicleType + ", power=" + power + ", emission=" + emission
					+ ", licenseType=" + licenseType + ", validity=" + validity + ", retirementDate=" + retirementDate
					+ ", capacity=" + capacity + ", color=" + color + "]";
		}
	}

	public static class ReadOpResult {
		private int opResult;
		private String data;

		public int getOpResult() {
			return opResult;
		}

		public void setOpResult(int opResult) {
			this.opResult = opResult;
		}

		public String getData() {
			return data;
		}

		public void setData(String data) {
			this.data = data;
		}
	}

	@Override
	public String toString() {
		return "Tag [tid=" + tid + ", discoveryTime=" + discoveryTime + ", vehicleInfos=" + vehicleInfos
				+ ", readOpResults=" + readOpResults + "]";
	}
}
